/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    /**
     * 自动设定值
     * @type type
     */
        Ext.onReady(function(){
           var grid_logs;                                                                              
    var record_logs;                                                                          
    var demId;                                                                                
    var store_setValues;                                                                      
    var store_setValues                                                                       
    var xg = Ext.grid;                                                                        
    var psize_setValues = 22;                                                                 
    var openUrl_setValues = basePath                                                          
            + 'JarpListener?className=cn.com.jsepc.rep.setvalue.SetValueDAO';                 
    openUrl_setValues += '&methodName=getGridData&paramCount=3';                              
                                                                                              
    store_setValues = new Ext.data.JsonReader({                                               
        root: 'data',                                                                         
        totalProperty: 'totalCount',                                                          
        fields: [{                                                                            
                name: 'id'                                                                    
            }, {                                                                              
                name: 'repid'                                                                 
            }, {                                                                              
                name: 'repname'                                                               
            }, {                                                                              
                name: 'roleid'                                                                
            }, {                                                                              
                name: 'time'                                                                  
            }, {                                                                              
                name: 'cyfl_id'                                                               
            }, {                                                                              
                name: 'yearmonthid'                                                           
            }, {                                                                              
                name: 'orgid'                                                                 
            }, {                                                                              
                name: 'cyfl_id_wan'                                                           
            }, {                                                                              
                name: 'yearmonthid_wan'                                                       
            }, {                                                                              
                name: 'orgid_wan'                                                             
            }, {                                                                              
                name: 'repvalue'                                                              
            }, {                                                                              
                name: 'sumvalue'                                                              
            }, {                                                                              
                name: 'trepval'                                                               
            }, {                                                                              
                name: 'tsumval'                                                               
            }, {                                                                              
                name: 'preval'                                                                
            }, {                                                                              
                name: 'tpreval'                                                               
            }, {                                                                              
                name: 'addtime'                                                               
            }, {                                                                              
                name: 'starttime'                                                             
            }, {                                                                              
                name: 'endtime'                                                               
            }]                                                                                
    });                                                                                       
                                                                                              
    store_setValues = new Ext.data.Store({                                                    
        autoLoad: false,                                                                      
        url: openUrl_setValues,                                                               
        reader: store_setValues,                                                              
        baseParams: {                                                                         
            param_0: repid,                                                                   
            param_1: 0,                                                                       
            param_2: psize_setValues,                                                         
        }                                                                                     
    });                                                                                       
                                                                                              
    var loadMarsk = new Ext.LoadMask(document.body, {                                         
        msg: '数据读取中...',                                                                      
        store: store_setValues                                                                
    });                                                                                       
    loadMarsk.show();                                                                         
                                                                                              
    store_setValues.setDefaultSort('id', 'ASC');                                              
    var sm_setValue = new xg.CheckboxSelectionModel({                                                  
        header: '',                                                                           
        singleSelect: true                                                                    
    });                                                                                       
                                                                                              
    var cm_setValue = new xg.ColumnModel([                                                             
        sm_setValue,                                                                                   
        {                                                                                     
            id: 'id',                                                                         
            name: 'id',                                                                       
            header: '编号',                                                                     
            sortable: true,                                                                   
            dataIndex: 'id',                                                                  
            hidden: true                                                                      
        },                                                                                    
        {                                                                                     
            name: 'repid',                                                                    
            header: '指标编号',                                                                   
            sortable: true,                                                                   
            dataIndex: 'repid',                                                               
            hidden: true                                                                      
        },                                                                                    
        {                                                                                     
            name: 'repname',                                                                  
            header: '指标名称',                                                                   
            sortable: true,                                                                   
            dataIndex: 'repname'                                                              
        },                                                                                    
        {                                                                                     
            name: 'roleid',                                                                   
            header: '用户',                                                                     
            sortable: true,                                                                   
            dataIndex: 'roleid'                                                               
        },                                                                                    
        {                                                                                     
            name: 'cyfl_id',                                                                  
            header: '产业分类',                                                                   
            sortable: true,                                                                   
            dataIndex: 'cyfl_id'                                                              
        },                                                                                    
        {                                                                                     
            name: 'time',                                                                     
            header: '操作时间',                                                                   
            sortable: true,                                                                   
            dataIndex: 'time'                                                                 
        },                                                                                    
        {                                                                                     
            name: 'yearmonthid',                                                              
            header: '年月',                                                                     
            sortable: true,                                                                   
            dataIndex: 'yearmonthid'                                                          
        },                                                                                    
        {                                                                                     
            name: 'orgid',                                                                    
            header: '单位',                                                                     
            sortable: true,                                                                   
            dataIndex: 'orgid'                                                                
        },                                                                                    
        {                                                                                     
            name: 'cyfl_id_wan',                                                              
            header: '产业分类(完成值)',                                                              
            sortable: true,                                                                   
            dataIndex: 'cyfl_id_wan'                                                          
        },                                                                                    
        {                                                                                     
            name: 'yearmonthid_wan',                                                          
            header: '年月(完成值)',                                                                
            sortable: true,                                                                   
            dataIndex: 'yearmonthid_wan'                                                      
        },                                                                                    
        {                                                                                     
            name: 'orgid_wan',                                                                
            header: '单位(完成值)',                                                                
            sortable: true,                                                                   
            dataIndex: 'orgid_wan'                                                            
        },                                                                                    
        {                                                                                     
            name: 'repval',                                                                   
            header: '当期值',                                                                    
            sortable: true,                                                                   
            dataIndex: 'repvalue'                                                             
        },                                                                                    
        {                                                                                     
            name: 'sumval',                                                                   
            header: '累计值',                                                                    
            sortable: true,                                                                   
            dataIndex: 'sumvalue'                                                             
        },                                                                                    
        {                                                                                     
            name: 'trepvalue',                                                                
            header: '同期值',                                                                    
            sortable: true,                                                                   
            dataIndex: 'trepval'                                                              
        },                                                                                    
        {                                                                                     
            name: 'tsumvalue',                                                                
            header: '同期累计',                                                                   
            sortable: true,                                                                   
            dataIndex: 'tsumval'                                                              
        },                                                                                    
        {                                                                                     
            name: 'preval',                                                                   
            header: '上期值',                                                                    
            sortable: true,                                                                   
            dataIndex: 'preval'                                                               
        },                                                                                    
        {                                                                                     
            name: 'tpreval',                                                                  
            header: '上期累计',                                                                   
            sortable: true,                                                                   
            dataIndex: 'tpreval'                                                              
        },                                                                                    
        {                                                                                     
            name: 'addtime',                                                                  
            header: '添加时间',                                                                   
            sortable: true,                                                                   
            dataIndex: 'addtime',                                                             
            hidden: true                                                                      
        },                                                                                    
        {                                                                                     
            name: 'starttime',                                                                
            header: '开始时间',                                                                   
            sortable: true,                                                                   
            dataIndex: 'starttime'                                                            
        },                                                                                    
        {                                                                                     
            name: 'endtime',                                                                  
            header: '结束时间',                                                                   
            sortable: true,                                                                   
            dataIndex: 'endtime'                                                              
        }                                                                                     
    ]);                                                                                       
                                                                                              
    cm_setValue.defaultSortable = true;           
    
    grid_logs = new Ext.grid.GridPanel({                                                      
        //autoHeight:true,                                                                    
        region: 'center',                                                                     
        title: '任务列表',                                                                        
        deferredRender: false,                                                                
        activeTab: 0,                                                                         
        split: true,                                                                          
        frame: true,                                                                          
        id: 'chc',                                                                            
        store: store_setValues,                                                               
        sm: sm_setValue,                                                                               
        cm: cm_setValue,                                                                               
        viewConfig: {                                                                         
            forceFit: false                                                                   
        },                                                                                    
        enableColumnResize: true, //控制列大小调节                                                   
        enableColumnMove: true, //控制列的拖动                                                      
        // loadMask:true,//数据加载时的遮罩功能                                                         
        stripeRows: true, //显示行的分隔符                                                           
        columnLines: true,                                                                    
        // height: screen.height - 320,                                                       
        stateful: true,                                                                       
        stateId: 'grid11',                                                                    
        bbar: new Ext.PagingToolbar({                                                         
            store: store_setValues,                                                           
            paramNames: {                                                                     
                start: 'param_1',                                                             
                limit: psize_setValues                                                        
            },                                                                                
            emptyMsg: '没有数据需要显示',                                                             
            // displayMsg : '显示第{0}到{1}行，共{2}行',                                              
            displayInfo: true,                                                                
            pageSize: psize_setValues                                                         
        })                                                                                    
    });                   
    
    store_setValues.reload();        

         
    
        var setValue = new Ext.FormPanel({
        id: 'setValue',
        labelWidth: 80,
        width: '98%',
        height: '100%',
        bodyStyle: 'padding: 4px',
        border: false,
        autoscroll: true,
        frame: true,
        autoScroll: true,
        items: [{
                title: '自动设定值',
                layout: 'form',
                autoscroll: true,
                xtype: 'fieldset',
                collapsible: true,
                collapsed: false,
                labelAlign: 'right',
                height: '100%',
                defaults: {
                    width: '100%'
                },
                items: [{
                        xtype: "textfield",
                        fieldLabel: "当期值",
                        emptyText: '当期值',
                        disabled: false,
                        //columnWidth: .3,
                        id: 'bianhao_repval',
                        anchor: "70%"
                    }, {
                        xtype: "textfield",
                        fieldLabel: "累计值",
                        emptyText: '累计值',
                        disabled: false,
                        //columnWidth: .3,
                        id: 'bianhao_sumval',
                        anchor: "70%"
                    }, {
                        xtype: "textfield",
                        fieldLabel: "同期值",
                        //columnWidth: .3,
                        emptyText: '同期值',
                        disabled: false,
                        id: 'bianhao_trepval',
                        anchor: "70%"
                    }, {
                        xtype: "textfield",
                        fieldLabel: "同期累积",
                        //columnWidth: .3,
                        emptyText: '同期累计值',
                        disabled: false,
                        id: 'bianhao_tsumval',
                        anchor: "70%"
                    }, {
                        xtype: "textfield",
                        fieldLabel: "上期值",
                        emptyText: '上期值',
                        disabled: false,
                        //columnWidth: .3,
                        id: 'bianhao_preval',
                        anchor: "70%"
                    }, {
                        xtype: "textfield",
                        fieldLabel: "上期累计",
                        //columnWidth: .3,
                        emptyText: '上期累计值',
                        disabled: false,
                        id: 'bianhao_tpreval',
                        anchor: "70%"
                    }, {
                        xtype: 'datefield',
                        fieldLabel: '开始时间',
                        name: 'starttime_date',
                        id: 'starttime_date',
                        altFormats: 'Ymd',
                        format: 'Y-m-d',
                        dateFormat: 'Ymd',
                        //columnWidth: .9,
                        emptyText: '任务开始时间',
                        anchor: "70%"
                    }, {
                        xtype: 'datefield',
                        fieldLabel: '结束时间',
                        name: 'endtime_date',
                        id: 'endtime_date',
                        altFormats: 'Ymd',
                        format: 'Y-m-d',
                        dateFormat: 'Ymd',
                        emptyText: '任务结束时间',
                        //columnWidth: .9,
                        anchor: "70%"
                    }]
            }]
    });
    /**
     * 
     * @type @exp;Ext@call;Window
     * 用于维度过滤
     */
    var shuju_win = new Ext.Window({
        id: 'shuju_win',
        title: "维度过滤(可不选时间维度)",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 435,
        // height:auto,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 确定 ',
                handler: function() {
                    //进行数据操作
                    var repval = Ext.ComponentMgr.get("bianhao_repval").getValue();
                    var sumval = Ext.ComponentMgr.get("bianhao_sumval").getValue();
                    var preval = Ext.ComponentMgr.get("bianhao_preval").getValue();
                    var tpreval = Ext.ComponentMgr.get("bianhao_tpreval").getValue();
                    var trepval = Ext.ComponentMgr.get("bianhao_trepval").getValue();
                    var tsumval = Ext.ComponentMgr.get("bianhao_tsumval").getValue();
                    var start = dateFormat(Ext.ComponentMgr.get("starttime_date").getValue());
                    var end = dateFormat(Ext.ComponentMgr.get("endtime_date").getValue());
                    if (repval == "") {
                        Ext.MessageBox.alert("错误", "请输入当期值！");
                        return;
                    }
                    if (sumval == "") {
                        Ext.MessageBox.alert("错误", "请输入累计值！");
                        return;
                    }
                    if (preval == "") {
                        Ext.MessageBox.alert("错误", "请输入上期值！");
                        return;
                    }
                    if (tpreval == "") {
                        Ext.MessageBox.alert("错误", "请输入上期累计值！");
                        return;
                    }
                    if (trepval == "") {
                        Ext.MessageBox.alert("错误", "请输入同期值！");
                        return;
                    }
                    if (tsumval == "") {
                        Ext.MessageBox.alert("错误", "请输入同期累计值！");
                        return;
                    }
                    if (start == "") {
                        Ext.MessageBox.alert("错误", "请选择开始时间！");
                        return;
                    }
                    if (end == "") {
                        Ext.MessageBox.alert("错误", "请选择结束时间！");
                        return;
                    }

                    var dimColValue = getExportDemlist();
                    if (dimColValue == "") {
                        Ext.MessageBox.alert("错误", "请筛选维度！");
                        return "";
                    }
                    var openUrl = basePath
                            + 'JarpListener?className=cn.com.jsepc.rep.report.AutoSetValue';
                    openUrl += '&methodName=setValue&paramCount=11&param_0=' + repid + '&param_1=' + roleid + '&param_2=' + getExportDemlist() + '&param_3=' + repval
                            + '&param_4=' + sumval + '&param_5=' + preval + '&param_6=' + tpreval + '&param_7=' + trepval + '&param_8=' + tsumval + '&param_9=' + start + '&param_10=' + end;
                    httpR_itemInfo = getXMLHttpRequest();
                    httpR_itemInfo.onreadystatechange = returnSetValue;
                    httpR_itemInfo.open("POST", openUrl, true);
                    httpR_itemInfo.send();
                    function returnSetValue() {
                        if (httpR_itemInfo.readyState == 4) {
                            if (httpR_itemInfo.status == 200) {
                                ret = httpR_itemInfo.responseText;
                                alert(ret);
                                 store_setValues.reload();
                            }
                        }
                    }
                    shuju_win.hide();
                }
            }, '-', {
                text: ' 关闭 ',
                handler: function() {
                    shuju_win.hide();
                }
            }, '-'],
        items: [shuju_weidu, shuju_pal, setValue]
    });





    //主视图
    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [{
                tbar: ['-', {
                        text: '添加任务',
                        iconCls: '',
                        handler: function() {
                            shuju_win.show();
                            shuju_win.doLayout();
                        }
                    }, '-', {
                        text: '删除任务',
                        handler: function() {
                            var re_grid = Ext.ComponentMgr.get("chc").getSelectionModel().getSelected();
                            if (re_grid) {
                                deleteFunction(re_grid.data.id);
                                store_setValues.reload();
                            } else {
                                Ext.Msg.alert('提示', '请选择任务!');
                            }
                        }
                    }, '-', {
                        text: '关闭窗口',
                        handler: function() {
                            window.close();
                        }
                    }, '-'
                ],
                region: 'center',
                deferredRender: false,
                activeTab: 0,
                frame: true,
                layout: 'border',
                items: [grid_logs]
            }]
    });

    function getExportDemlist() {
        var sender = {};
        var hideCol = [];
        var array = [];
        for (var i = 0; i < dimLen; i++) {
            var o = {};
            o.facdimtablewan = Ext.ComponentMgr.get("facdimtablewan" + i).getValue();
            o.facdimcodecolwan = Ext.ComponentMgr.get("facdimcodecolwan" + i).getValue();
            o.facdimvaluewan = Ext.ComponentMgr.get("facdimvaluewan" + i).getValue();
            o.facdimfieldwan = Ext.ComponentMgr.get("facdimfieldwan" + i).getValue();
            o.facdimtable_weidu = Ext.ComponentMgr.get("facdimtable_weidu" + i).getValue();
            o.facdimcodecol_weidu = Ext.ComponentMgr.get("facdimcodecol_weidu" + i).getValue();
            o.facdimvalue_weidu = Ext.ComponentMgr.get("facdimvalue_weidu" + i).getValue();
            //demArr[i]  = o;
            if (o.facdimvalue_weidu == "") {
                hideCol.push(o.facdimfield);
            }
            array.push(o);
        }
        if (dimLen == hideCol.length) {
            return "";
        }
        sender.demlist = array;
        // alert(Ext.util.JSON.encode(sender));
        return Ext.util.JSON.encode(sender);
    }

    /**
     * 
     * @param {type} value
     * @returns {@exp;Ext@pro;util@pro;Format@call;date}
     * 用来格式化时间
     */
    function dateFormat(value) {
        if (null != value && value != "") {
            return Ext.util.Format.date(new Date(value), 'Ymd');
        } else {
            return "";
        }
    }
    
    function deleteFunction(id){
         var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.setvalue.SetValueDAO';
                    openUrl += '&methodName=deleteFunction&paramCount=1&param_0=' + id ;
                    httpR_itemInfo = getXMLHttpRequest();
                    httpR_itemInfo.onreadystatechange = returnDeleteFunction;
                    httpR_itemInfo.open("POST", openUrl, true);
                    httpR_itemInfo.send();
                    function returnDeleteFunction() {
                        if (httpR_itemInfo.readyState == 4) {
                            if (httpR_itemInfo.status == 200) {
                                ret = httpR_itemInfo.responseText;
                                Ext.Msg.alert("提示",ret);
                            }
                        }
                    }
    }

    });
    